# encoding = utf-8
class TypesController < ApplicationController

  before_filter :authenticate_admin!, :only => [:new, :create, :list, :edit, :update, :delete]

  def new
    @type = Type.new
    @action = {:action => 'create'}
    @action_title = '添加新产品种类'
    render :layout => 'admins'
  end
  
  def create
    type = Type.new(params[:type])

    if type.save
      flash[:success] = '添加产品种类成功'
    else
      flash[:error] = '添加产品种类失败'
    end

    redirect_to :action => "list"
  end

  def list
    @types = Type.order('created_at desc').paginate :page => params[:page], :per_page => PAGE_SIZE
    render :layout => 'admins'
  end

  def edit
    @type = Type.find(params[:id])
    @action = {:action => 'update', :id => @type.id}
    @action_title = '编辑产品种类'
    render :layout => 'admins', :action => 'new'
  end

  def update
    type = Type.update(params[:id], params[:type])
    if type
      flash[:success] = '产品种类修改成功'
    end
    redirect_to :action => "list"
  end

  def delete
    type = Type.find(params[:id])
    if type.destroy
      flash[:success] = '产品种类删除成功'
    end
    redirect_to :action => "list"
  end
  
end
